package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.dto.CourseAddDto;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Mr.Alan
 * @since 2020-08-16
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Resource
    private EduChapterService eduChapterService;
    @Resource
    private EduVideoService eduVideoService;
    @Resource
    private VodClient vodClient;
    @Resource
    private EduTeacherService eduTeacherService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveCourseInfo(CourseAddDto courseAddDto) {

        Date now = new Date();
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseAddDto, course);
        course.setGmtCreate(now)
                .setGmtModified(now);
        EduCourseDescription desc = new EduCourseDescription();
        BeanUtils.copyProperties(courseAddDto, desc);
        desc.setGmtCreate(now)
                .setGmtModified(now);
        if (super.save(course)) {
            desc.setId(course.getId());
            eduCourseDescriptionService.save(desc);
            return course.getId();
        }
        return null;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateCourseInfo(CourseAddDto courseAddDto) {

        Date now = new Date();
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseAddDto, course);
        course.setGmtCreate(now)
                .setGmtModified(now);
        EduCourseDescription desc = new EduCourseDescription();
        BeanUtils.copyProperties(courseAddDto, desc);
        desc.setGmtCreate(now)
                .setGmtModified(now);
        if (super.updateById(course)) {
            desc.setId(course.getId());
            eduCourseDescriptionService.updateById(desc);
            return course.getId();
        }
        return course.getId();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeCourseById(String id) {

        /*
        edu_course
        edu_course_description
        edu_chapter
        edu_video
        todo 收藏表带处理
         */
        // 删除所有的视频
        List<String> videoIds = eduChapterService.lambdaQuery().eq(EduChapter::getCourseId, id).select(EduChapter::getId,
                EduChapter::getVideoId).list().stream().map(EduChapter::getVideoId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(videoIds)) {
            vodClient.deleteVideoById(videoIds);
        }
        eduCourseDescriptionService.remove(Wrappers.<EduCourseDescription>lambdaQuery().eq(EduCourseDescription::getId, id));
        eduChapterService.remove(Wrappers.<EduChapter>lambdaQuery().eq(EduChapter::getCourseId, id));
        eduVideoService.remove(Wrappers.<EduVideo>lambdaQuery().eq(EduVideo::getCourseId, id));
        return super.removeById(id);
    }

    @Override
    public boolean offShelfCourse(String id) {
        EduCourse course = new EduCourse();
        course.setId(id)
                .setStatus("Draft");
        return super.updateById(course);
    }

    @Override
    public boolean offShelfCourseByChapterId(String chapterId) {
        EduChapter dbData = eduChapterService.lambdaQuery().eq(EduChapter::getId, chapterId).select(EduChapter::getId, EduChapter::getCourseId).one();
        if (ObjectUtils.isNotNull(dbData)) {
            return offShelfCourse(dbData.getCourseId());
        }
        return false;
    }

    @Override
    public CourseAddDto getInfoById(String id) {
        EduCourse course = super.lambdaQuery().eq(EduCourse::getId, id).one();
        if (ObjectUtils.isNotNull(course)) {
            CourseAddDto dto = new CourseAddDto();
            BeanUtils.copyProperties(course, dto);
            EduCourseDescription desc = eduCourseDescriptionService.lambdaQuery().eq(EduCourseDescription::getId, id).one();
            if (ObjectUtils.isNotNull(desc)) {
                dto.setDescription(desc.getDescription());
            }
            dto.setSubjectIds(dto.getSubjectIds());
            return dto;
        }
        return null;
    }

    @Override
    public Map<String, Object> getCourseInfo(String id) {

        EduCourse course = super.lambdaQuery().eq(EduCourse::getId, id)
                .select(EduCourse::getId, EduCourse::getTitle, EduCourse::getViewCount, EduCourse::getBuyCount,
                        EduCourse::getLessonNum, EduCourse::getPrice, EduCourse::getCover, EduCourse::getTeacherId).one();

        EduCourseDescription desc = eduCourseDescriptionService.lambdaQuery().eq(EduCourseDescription::getId, id).one();

        EduTeacher teacher = eduTeacherService.lambdaQuery().eq(EduTeacher::getId, course.getTeacherId())
                .select(EduTeacher::getId, EduTeacher::getName, EduTeacher::getAvatar, EduTeacher::getIntro).one();

        List<EduChapter> eduChapters = eduChapterService.listByCourseId(course.getId());

        Map<String, Object> result = Maps.newHashMap();
        result.put("course", course);
        result.put("desc", desc);
        result.put("teacher", teacher);
        result.put("eduChapters", eduChapters);

        return result;
    }


}
